Skip to main content

What is an API? A Simple Explanation for Programming Beginners


API

What is an interface?

Interface is a control system, through which we can use something.

For Example:
The interface of car includes:
  • Steering Wheel
  • Brakes
  • Gear
  • Acceleration pedal
  • Mirrors
Interface is concerned with the fact that how to use something, rather than how it works.

The Easiest Analogy for an API: The Restaurant

  • Imagine you're at a restaurant.
  • You are the user. You want food (data or a service).
  • You can't just walk into the kitchen and start cooking. That would be chaotic and unsafe! Instead, you interact with the waiter.
  • You look at a menu (the API documentation) and give your order to the waiter. The waiter takes your request to the kitchen. The kitchen prepares your food. The waiter then brings the response (your delicious meal) back to you.
The waiter is the contract, the messenger, the interface in our case the API. They provide a secure and simple way for you to get what you need from the kitchen without needing to know how the kitchen works.

What is an API?

API is a collection of functions which are public in our program.

Application programming Interface is a contract or a program containing the functions which are to be used by the user.

Actually, when we write a program, we don't give our code to the user, we keep our code at our device so that it can't be changed or can't be stolen. So, if user doesn't get the code, how would he be able to use our website or online service then? Here comes the API.

How API is used behind the scenes?

We make a frontend website, typically of HTML, CSS and JavaScript containing just buttons and interactive fields. Nothing more. It does not contain actual algorithms or complex logic. So when user clicks on the any of the buttons or perform any action, a corresponding API request is generated behind the scene,  API which is present on server side, receives that request, calls the appropriate function based on the request and returns the response to the browser or application from where API was called.

Benefits:

  • The first and foremost benefit which we get is the security, we don't need to give our complete code to the user. We just get the request and give the response. It keeps the direct contact to the actual code or database. 
  • The API class just has access to those functions which are needed by the user. It does not know the actual implementation, it just calls the function.





Disclaimer: This is an example of java API using basic example. In reality, this we use HTTP request and Spring Boot for creating API. We have taken this example for simplicity.


As we can see that our API is just calling the functions like removeStudent() and addStudent() and getting the response. The function implementation is in another class and that class is not exposed directly to the API, so someone from outside would not be able to access the logic and change that logic.

The actual implementation of the class StudentDatabase is below:




The actual implementation is in this StudentDatabase class and it is not exposed directly.

Conclusion:

As you can see, our StudentAPI  is the perfect waiter. It takes the order, passes it to the kitchen, and returns the result, all without exposing any of the secret logic inside the StudentDatabase class.

That, in a nutshell, is what an API does. It's a secure and powerful contract that allows different pieces of software to talk to each other.

Comments

Popular posts from this blog

Java Servlets

  According to me its just a translator, who manages the HTTP requests and handle them to the appropriate function of the API. So then, API turns out to be a term or set of those functions which are kept public. Now let us see in little bit of detail what servlets are: For that we need to see few terms: Static Web Pages: Static web pages are the pages, which are already built, and whose content and design always remain the same.  For Example: About us page of some website. Contact info page of some website Dynamic Web Pages: Dynamic pages are not already defined. They are dependent on search queries. For Example: If we search top 5 books, and click on search button, result would be different. If we search top 5 countries and enter search, result would be different.  So, the search result for every search is different. Working: Now if we have to search static page let's say the first page of any website (which is same for every use in every country) then the process is sim...

OnClick and {}

1. The onClick Mystery: Is it the same? Yes, the concept is 90% the same. If you know how to handle a click in plain HTML/JavaScript, you already know how to do it in React. The logic flow is identical: Event: A user does something (Click). Listener: The element hears it ( onClick ). Handler: A specific function runs to deal with it. The "React Twist" (The 10% difference): There are two tiny syntax differences you must memorize: CamelCase: In HTML, it is onclick (lowercase). In React, it is onClick (camelCase). Function, not String: HTML (Old): onclick="doSomething()" (A string of code). React (New): onClick={doSomething} (Passing the actual function itself). So yes, your intuition is correct: Whenever you need to handle logic (clicks, hovers, typing), you will stick to this pattern: on[Event]={functionName} . 2. The Curly Braces { } : The Portal This is the most important syntax rule in JSX. Imagine your code file has two "Modes": HTML Mode: W...

Servlet Context and Servlet Config

  Problem without init-parameters? Imagine you write a servlet that connects to a database. If you write the database URL, username, and password directly in your Java code, you have a big problem: What if the database password changes? You have to find the code, change it, recompile the .java file into a .class file, and redeploy the entire application. What if you want to use the same servlet in a different project with a different database? You can't. The code is stuck with the old settings. This is where init parameters come in. They let you move this configuration data out of your code and into the deployment descriptor ( web.xml file). How init-parameters help when we use them in web.xml file? They help us in a way that we don't have to find that particular part of code to change it. And also, it saves us the hectic of changing source code and recompiling that again. Anyone with .war file can change the web.xml file  and use it with class file without having the ne...